<template>
  <div class="container">
    <van-nav-bar fixed title="登录" @click-left="$router.back()" left-arrow />

    <div class="logo-txt">优点达资讯</div>
    <van-form @submit="onSubmit" label-width="50px">
      <van-field
        v-model="username"
        name="mobile"
        label="用户名"
        placeholder="请输入手机号"
        left-icon="manager"
        :rules="[{ required: true, message: '请填写用户名' }]"
      />
      <van-field
        v-model="password"
        type="password"
        name="password"
        label="密码"
        placeholder="密码"
        left-icon="lock"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 16px">
        <van-button round block type="info" native-type="submit"
          >提交</van-button
        >
      </div>
    </van-form>
    <van-row type="flex" justify="end">
      <van-col :span="10">
        <router-link to="/reg">没有账号,立即注册</router-link>
      </van-col>
    </van-row>
  </div>
</template>

<script>
// 导入封装 的API 函数
import { loginApi } from "@/api/user";
import { setItem } from '@/utils/storage'
export default {
  data() {
    return {
      username: "",
      password: "",
    };
  },
  methods: {
    async onSubmit(values) {
      const { data, errmsg, errno } = await loginApi(values);
      // console.log(data);
      if (errno !== 0) return this.$toast(`${errmsg},请重新输入`);
      if (errno === 0) {
        this.$toast(errmsg)
        setItem('token-ydd', data)
        // 跳转的主页
        this.$router.replace('/user')
      }
    },
  },
};
</script>

<style scoped>
.logo-txt {
  text-align: center;
  font-size: 60px;
  margin: 60px auto;
  color: #1989fa;
}
</style>
